package handlers

import (
	models2 "02/models"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"strconv"
)

func Add(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		var travel models2.Travel
		Title := c.PostForm("Title")
		if Title == "" {
			c.JSON(200, gin.H{
				"code": 0,
				"msg":  "标题不能为空",
			})
			return
		}
		Content := c.PostForm("Content")
		Img := c.PostForm("Img")
		UserId, _ := strconv.Atoi(c.PostForm("UserId"))
		travel = models2.Travel{
			Title:   Title,
			Content: Content,
			Img:     Img,
			UserId:  UserId,
		}
		err := db.Create(&travel).Error
		if err != nil {
			c.JSON(200, gin.H{
				"code": 0,
				"msg":  "添加失败",
			})
			return
		} else {
			c.JSON(200, gin.H{
				"code": 200,
				"msg":  "添加成功",
			})
			return
		}
	}
}
func Del(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		id := c.Query("id")
		err := db.Delete(&models2.Travel{}, id).Error
		if err != nil {
			c.JSON(200, gin.H{
				"code": 0,
				"msg":  "删除失败",
			})
			return
		} else {
			c.JSON(200, gin.H{
				"code": 200,
				"msg":  "删除成功",
			})
			return
		}
	}

}
